using FreeSql.DataAnnotations;
using SumerCoreDevOps.Shared.Enums;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 流水线实例
/// </summary>
[Table(Name = "pipeline_instances")]
[Index("idx_pipeline", nameof(PipelineId), false)]
[Index("idx_status", nameof(Status), false)]
public class PipelineInstance
{
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 流水线ID
    /// </summary>
    public long PipelineId { get; set; }

    /// <summary>
    /// 构建编号
    /// </summary>
    public int BuildNumber { get; set; }

    /// <summary>
    /// 状态
    /// </summary>
    public Shared.Enums.TaskStatus Status { get; set; } = Shared.Enums.TaskStatus.Pending;

    /// <summary>
    /// Git提交哈希
    /// </summary>
    [Column(StringLength = 100)]
    public string? CommitHash { get; set; }

    /// <summary>
    /// Git提交信息
    /// </summary>
    [Column(StringLength = 500)]
    public string? CommitMessage { get; set; }

    /// <summary>
    /// 触发人
    /// </summary>
    [Column(StringLength = 100)]
    public string? TriggeredBy { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime? StartTime { get; set; }

    /// <summary>
    /// 结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 流水线
    /// </summary>
    [Navigate(nameof(PipelineId))]
    public Pipeline Pipeline { get; set; } = null!;

    /// <summary>
    /// 任务列表
    /// </summary>
    [Navigate(nameof(TaskEntity.PipelineInstanceId))]
    public List<TaskEntity> Tasks { get; set; } = [];
}
